package com.project.lk.nurseapp.babyvoice;

import android.util.Log;

import java.io.File;
import java.io.RandomAccessFile;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/3
 * 修改日期：
 */

public class FetalHeartRate
{




    public static class State
    {
        short	valprev;
        int	index;
    };
    /*
    indata: 输入的数据，含有一整帧的
    outdata：解析后得到的音频数据
    len：一帧数据的数据长度

    返回：
    -1：该帧有错
    其它：胎心率
    */
    private static int decoder(byte indata[], short outdata[], int len, State state)
    {
        int indataIndex;
        int outdataIndex;
        int delta; //unsigned int
        int step;
        int valpred;
        int vpdiff;
        int index;
        int inputbuffer = 0;//unsigned int
        int count = 0;

        valpred = state.valprev;
        //index = (int)(state.index & 0xFF);
        index = (int)state.index;
        step = stepsizeTable[index];

        indataIndex = 0;
        outdataIndex = 0;
        while ( len-- > 0 ) {
            inputbuffer = (indata[indataIndex] & 0xFF);
            indataIndex++;
            delta = (inputbuffer >> 4);

            index += indexTable[delta];
            if ( index < 0 ) index = 0;
            else if ( index > 88 ) index = 88;

            vpdiff = step >> 3;
            if ( (delta & 4) != 0 ) vpdiff += step;
            if ( (delta & 2) != 0 ) vpdiff += step>>1;
            if ( (delta & 1) != 0 ) vpdiff += step>>2;

            if ( (delta & 8) != 0 )
            {
                valpred -= vpdiff;
                if ( valpred < -32768 )
                    valpred = -32768;
            }
            else
            {
                valpred += vpdiff;
                if ( valpred > 32767 )
                    valpred = 32767;
            }

            step = stepsizeTable[index];

            outdata[outdataIndex] = (short)valpred;
            outdataIndex++;

            delta = inputbuffer & 0xf;

            index += indexTable[delta];
            if ( index < 0 ) index = 0;
            else if ( index > 88 ) index = 88;

            vpdiff = step >> 3;
            if ( (delta & 4) != 0 ) vpdiff += step;
            if ( (delta & 2) != 0 ) vpdiff += step>>1;
            if ( (delta & 1) != 0 ) vpdiff += step>>2;

            if ( (delta & 8) != 0 )
            {
                valpred -= vpdiff;
                if ( valpred < -32768 )
                    valpred = -32768;
            }
            else
            {
                valpred += vpdiff;
                if ( valpred > 32767 )
                    valpred = 32767;
            }

            step = stepsizeTable[index];

            outdata[outdataIndex] = (short)valpred;
            outdataIndex++;
            count += 2;
        }

        state.valprev = (short)valpred;
        state.index = (byte)index;
        //state.index = index;

        return count;
    }

    /*
    indata: 输入的数据，含有一整帧的
    outdata：解析后得到的音频数据
    len：一帧数据的数据长度
    state：解码的状态标志，全局变量,初始化为0

    返回：
    -1：该帧有错
    其它：fhr
    */
    public static   int decode_frame(byte indata[], short outdata[], int len)
    {
        int ret = -1;
        int sum;
        int i;
        int count;
        int status;
        byte temp[] = new byte[len - 8];
        State sta = new State();

        if (((indata[0] & 0xFF) == 0xFF) && ((indata[1] & 0xFF) == 0xAA) && ((indata[len - 1] & 0xFF) == 0x55))
        {//帧头帧尾都对，是正常的一帧数据
            sum = indata[0];
            for (i = 1; i < len - 2; i++)
            {
                sum += (indata[i] & 0xFF);
            }
            sum = sum  & 0xFF;

            //校验正确
            if (sum == (indata[len - 2] & 0xFF))
            {
                ret = (indata[5] & 0xFF);
                for(i = 6; i < len - 2; i++)
                {
                    temp[i - 6] = (indata[i]);
                }

                sta.index = (int)(indata[4] & 0xFF);
                status = (indata[3] & 0xFF);
                status <<= 8;
                status |= (indata[2] & 0xFF);
                sta.valprev = (short)status;

                count = decoder(temp, outdata, len - 8, sta);
            }
        }
        return ret;
    }
    //static final int indexTable[16] =
    static final int indexTable[] =
            {
                    -1, -1, -1, -1, 2, 4, 6, 8,
                    -1, -1, -1, -1, 2, 4, 6, 8,
            };

    // static final int stepsizeTable[89]=
    static final int stepsizeTable[]=
            {
                    7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
                    19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
                    50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
                    130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
                    337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
                    876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
                    2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
                    5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
                    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
            };




    public static int  processInComingFrameData(byte[] indata,short[] outdata) {
//        short[] outdata = new short[100];
        int fhr  = decode_frame(indata,outdata,58);
        if( fhr < 0 )
        {
            //error !
            Log.e("MLOG","processInComingFrameData => decode_frame error");
            return -1;
        }
        return fhr;
    }
}

